/*
 * @Author: shiguo
 * @Date: 2022-07-11 15:42:16
 * @LastEditors: shiguo
 * @LastEditTime: 2022-07-11 16:15:02
 * @FilePath: /@aks-dev/easyui/lib/RichText/RichText.tsx
 */
import React from 'react'
import { StyleSheet, Text } from 'react-native'
import type { RichTextProps } from '.'
import { px2sp } from '../../screen/px2sp'
import * as utils from '../../utils/lazy'




export default (props: RichTextProps) => {
    const { rich, richStyle, text, suffix, prefix, ...rest } = props
    let textStr = ''
    let richStr = ''
    if (utils.isString(text) || utils.isNumber(text)) {
        textStr = String(text)
    }
    if (utils.isString(rich) || utils.isNumber(rich)) {
        richStr = String(rich)
    }

    let positionStart = 0
    let positionEnd = 0
    if (textStr?.indexOf(richStr) >= 0) {
        positionStart = textStr.indexOf(richStr)
        positionEnd = positionStart + richStr.length
    }

    if (prefix) {
        return (
            <Text style={styles.container} {...rest}>
                <Text style={{ ...styles.defaultRich, ...richStyle }}>{richStr}</Text>
                {textStr}
            </Text>
        )
    }



    if (suffix) {
        return (
            <Text style={styles.container} {...rest}>
                {textStr}
                <Text style={{ ...styles.defaultRich, ...richStyle }}>{richStr}</Text>
            </Text>
        )
    }


    return (
        <Text style={styles.container} {...rest}>
            {textStr.substring(0, positionStart)}
            {
                positionEnd > 0 &&
                <Text style={{ ...styles.defaultRich, ...richStyle }}>{richStr}</Text>
            }
            {textStr.substring(positionEnd, textStr.length)}
        </Text>
    )
}





const styles = StyleSheet.create({
    container: {
        color: '#333333',
        fontSize: px2sp(14)
    },
    defaultRich: {
        color: 'red',
        fontSize: px2sp(14),
        // fontWeight: 'bold'
    }
})